import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Cette classe permet d'afficher la fenêtre de promotion du pion.
 * Le joueur concerné pourra choisir la pièce qu'il désire
 *  
 **/

public class PromotionFenetre extends JFrame implements MouseListener, WindowListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel panel = new JPanel();
	private JLabel tour ; 
	private JLabel fou ;
	private JLabel reine ; 
	private JLabel cavalier ;
	
	/*Image des Pièces*/
	/*Blancs*/
	final ImageIcon tourB = createImageIcon("/images2/tourb.png");
	final ImageIcon fouB = createImageIcon("/images2/foub.png");
	final ImageIcon cavalierB = createImageIcon("/images2/cavalierb.png");
	final ImageIcon reineB = createImageIcon("/images2/dameb.png");
	/*Noirs*/
	final ImageIcon tourN = createImageIcon("/images2/tourn.png");
	final ImageIcon fouN = createImageIcon("/images2/foun.png");
	final ImageIcon cavalierN = createImageIcon("/images2/cavaliern.png");
	final ImageIcon reineN = createImageIcon("/images2/damen.png");
	
	/*Attributs temporaires - utilisés en paramètre du constructeur*/
	private boolean Blanc;
	private Alice a;
	private Piece p;
	private Fenetre fTmp;
	
	public PromotionFenetre(boolean blanc, Alice al, Piece pi,Fenetre f)
	{
		setLocation(350, 300);
        setLayout(new BorderLayout());
        panel.setLayout(new GridLayout(1,4));
        setTitle("Choix de la nouvelle Piece");
        setVisible(true);
        
        /*Affectation valeurs récupérés en paramètre aux attributs temporaires*/
        Blanc = blanc;
        a = al;
        p = pi;
        
        fTmp = f;
        fTmp.disable();
        
        /*Configuration des éléments du panel de la fenêtre*/
        if (blanc)
        {
        	tour = new JLabel(tourB);
        	reine = new JLabel(reineB);
        	fou = new JLabel(fouB);
        	cavalier = new JLabel(cavalierB);
        }
        else
        {
        	tour = new JLabel(tourN);
        	reine = new JLabel(reineN);
        	fou = new JLabel(fouN);
        	cavalier = new JLabel(cavalierN);
        }
        
        /*Ajout des éléments au panel*/
        panel.add(tour);
        panel.add(fou);
        panel.add(cavalier);
        panel.add(reine);
        
        /*Ajout des interactions utilisateur*/
        tour.addMouseListener(this);
        fou.addMouseListener(this);
        cavalier.addMouseListener(this);
        reine.addMouseListener(this);
        
        addWindowListener(this);		/*Fermeture peronalisée*/
        
        /*Affichage*/
        this.getContentPane().add(panel);
        this.pack();    
        this.setSize(350, 100);
	}
	
	/*Retourne une ImageIcon, null si le chemin d'accès est invalide*/
    protected static ImageIcon createImageIcon(String path)
	{
        java.net.URL imgURL = Alice.class.getResource(path);
        if (imgURL != null) 
		{
        	
            return new ImageIcon(imgURL);
        } 
		else
		{
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

	public void mouseClicked(MouseEvent e) 
	{
		/*envoie du type de piece à la classe Alice*/
		if (e.getSource() == tour)
		{
			/*modifier tour*/
			if (Blanc)
			{
				System.out.println(p);
				p.setM_type("tb");
			}
			else p.setM_type("tn");
		}
		else if (e.getSource() == fou)
		{
			/*modifier fou*/
			if (Blanc)
				p.setM_type("fb");
			else p.setM_type("fn");
		}
		else if (e.getSource() == cavalier)
		{
			/*modifier cavalier*/
			if (Blanc)
				p.setM_type("cb");
			else p.setM_type("cn");
		}
		else if (e.getSource() == reine)
		{
			/*modifier reine*/
			if (Blanc)
				p.setM_type("rb");
			else p.setM_type("rn");
		}
		
		a.getLayeredPane().removeAll();
		a.afficherPiecesJoueurs(false);
		a.getLayeredPane().repaint();
		
		System.out.println("dans promF"+p.getM_type());
		this.dispose();
		fTmp.enable();
	}
	
	public void windowClosing(WindowEvent e) {
		close();
	  }
	
	public void close()
	{
		dispose(); /*Fermeture de la fenêtre*/
		fTmp.enable();
	}

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub	
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
	}

	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub	
	}

	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
	}

	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
	}
	
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
	}
}
